<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Exercises
 *
 * @author BLIND KNIGHT
 */
class ExercisesLibrary {

    public static function generateTest($questionNumer) {
        $questionsList = array();
        for ($i = 1; $i <= 7; $i++) {
            $limit = $questionNumer[$i] ? $questionNumer[$i] : 0;
            $parameters = array(
                "part = '$i'",
                'order' => 'RAND()',
                'limit' => $limit,
            );

            $qtList = array();
            switch ($i) {
                case 1:
                case 2:
                    $questions = Questions::find($parameters);
                    foreach ($questions as $value) {
                        $questionsList[] = $value->id;
                    }
                    break;
                case 3:
                case 4:
                    $audios = Audios::find($parameters);
                    foreach ($audios as $audio) {
                        $parameters[0] = "audio = '$audio->id'";
                        $questions = Questions::find("audio= '$audio->id' AND part='$i'");
                        foreach ($questions as $value) {
                            $questionsList[] = $value->id;
                        }
                    }
                    break;
                case 5:
                    $questions = Questions::find($parameters);
                    foreach ($questions as $value) {
                        $questionsList[] = $value->id;
                    }
                    break;
                case 6:
                    $qtList = array();
                    $documents = Documents::find($parameters);
                    foreach ($documents as $reference) {
                        $questions = Questions::find("document ='$reference->id' AND part='$i'");
                        foreach ($questions as $value) {
                            $questionsList[] = $value->id;
                        }
                    }
                    break;
                case 7:
                    $qtList = array();

                    $count = 0;
                    $parameters['limit'] = $part1Limit = floor(($limit[1] ? $limit[1] : 0) / 2);
                    $parameters[0] = "part = '$i' AND paragraphs = '1'";
                    $documents = Documents::find($parameters);
                    foreach ($documents as $referenceKey => $reference) {
                        $questions = Questions::find("document ='$reference->id' AND part='$i'");
                        foreach ($questions as $questionKey => $question) {
                            if (($questionKey >= 2 && $questionKey > (count($questions) - 2) && $count == ($limit[1] - 2)) || $count == $part1Limit) {
                                break;
                            } else {
                                foreach ($questions as $value) {
                                    $questionsList[] = $value->id;
                                }
                                $count++;
                            }
                        }
                        if ($count == $part1Limit) {
                            break;
                        }
                    }

                    $parameters[0] = "part = '$i' AND paragraphs = '2'";
                    $parameters['limit'] = $limit[2] ? $limit[2] : 0;
                    $documents = Documents::find($parameters);
                    foreach ($documents as $reference) {
                        $questions = Questions::find("document = '$reference->id' AND part='$i'");
                        foreach ($questions as $value) {
                            $questionsList[] = $value->id;
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        return $questionsList;
    }

}
